package com.mallcai.bigdata.ladon.service.export.impl;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.client.export.ExportResultVO;
import com.mallcai.bigdata.ladon.client.export.ExportStrategy;
import com.mallcai.bigdata.ladon.client.param.ReportQueryParam;
import com.mallcai.bigdata.ladon.client.service.IExportService;
import com.mallcai.bigdata.ladon.client.service.IReportDataQueryService;
import com.mallcai.bigdata.ladon.client.vo.ReportDataVO;
import com.mallcai.bigdata.ladon.dpl.entity.export.ExportTask;
import com.mallcai.bigdata.ladon.dpl.entity.report.Report;
import com.mallcai.bigdata.ladon.service.export.ITaskService;
import com.mallcai.bigdata.ladon.service.report.IReportService;
import com.mallcai.bigdata.ladon.utils.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * described :
 * Created by CaKeDou on 2019-10-28 15:25.
 * Modify:
 */
//@Service(interfaceClass = IExportService.class, version = "1.0.0")
@Component
public class ExportServiceImpl implements IExportService {

    @Autowired
    private IReportDataQueryService iReportDataQueryService;
    @Autowired
    private IReportService          reportService;
    @Autowired
    private ExportPipeline          exportPipeline;
    @Autowired
    private ITaskService            taskService;

    @Override
    public Long createTask(String appName, String reportCode, Map<String, Object> param) {
        ReportDataVO result = getQueryData(appName, reportCode, param);
        Report report = reportService.getReportByCode(reportCode);
        try {
            ExportResultVO exportResultVO = exportPipeline.doExport(report.getName(), report, ExportStrategy.builder(ExportStrategy.ExportFileTypeEnum.XLSX, new ExportStrategy.AsyncInfo(true)).saveToOss(true).build());
            return exportResultVO.getTaskId();
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
    }

    @Override
    public String getDownLoadUrl(Long taskId) {
        ExportTask task = taskService.getStatus(taskId);
        if (task.getTaskStatus() == 1) {
            return task.getDownloadUrl();
        } else if (task.getTaskStatus() == 0) {
            String errorMsg = task.getErrMsg();
            if (StringUtils.isBlank(errorMsg)) {
                throw new BusinessException("204", "文件正在生成中");
            } else {
                throw new BusinessException("500", errorMsg);
            }
        }
        return null;
    }

    @Override
    public <T> ExportResultVO consumerExport(String fileName, T oriData, ExportStrategy exportStrategy) {
        try {
            ExportResultVO exportResultVO = exportPipeline.doExport(fileName, oriData, exportStrategy);
            return exportResultVO;
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

    private ReportDataVO getQueryData(String appName, String reportCode, Map<String, Object> params) {
        Set<String> ans = iReportDataQueryService.parseParamName(reportCode);
        Map<String, Object> allParams = new HashMap<>(8);
        if (CollectionUtils.isNotEmpty(params)) {
            allParams.putAll(params);
        }
        ReportQueryParam reportQueryParam = new ReportQueryParam(appName, reportCode);
        reportQueryParam.setExportReport(true);
        return iReportDataQueryService.queryData(reportQueryParam, allParams);
    }
}
